package com.google.shoopinggoods.fragment;

import android.support.v4.view.ViewCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.example.administrator.shoppinggoods.R;
import com.google.shoopinggoods.utils.KLog;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lxj.okhttpengine.OkHttpEngine;
import com.lxj.okhttpengine.PostParams;
import com.lxj.okhttpengine.callback.OkHttpCallback;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/14.
 */
public abstract class BaseListFragment extends BaseFragment implements XRecyclerView.LoadingListener {
    @BindView(R.id.recyclerView)
    com.jcodecraeer.xrecyclerview.XRecyclerView recyclerView;

    @BindView(R.id.iv_arrow)
    ImageView ivArrow;
    public RecyclerView.Adapter mAdapter;
    private int page;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_list;
    }

    @Override
    public void setListener() {
        recyclerView.setLoadingListener(this);
    }

    @Override
    public void init() {
        mAdapter = getAdapter();
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        recyclerView.setAdapter(mAdapter);
        recyclerView.addOnScrollListener(new ScrollListener());
        hideArrow();
    }

    //隐藏底部点击向上加载的按钮
    private void hideArrow() {
        ivArrow.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ivArrow.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                ivArrow.setTranslationY(200);
            }
        });
    }

    public abstract String getUrl();

    public abstract PostParams getPostParams();

    public abstract RecyclerView.Adapter getAdapter();

    @Override
    public void loadData() {
        String url = getUrl();//每件商品的详细信息
        KLog.e(KLog.Tag.QUICKEY,"BaseListFragment loadData url:"+url);
        if(getPostParams()==null){
            OkHttpEngine.create().get(url,getCallback());
        }else{
            OkHttpEngine.create().post(url,getPostParams(),getCallback());
        }
    }

    public abstract OkHttpCallback getCallback();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public boolean isRefresh=false;
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 0;
        loadData();
    }

    @OnClick(R.id.iv_arrow)
    public void onClick(){
        recyclerView.smoothScrollToPosition(0);
        ViewCompat.animate(ivArrow).translationY(200).setDuration(500).start();
    }

    @Override
    public void onLoadMore() {
        loadData();
    }

    private class ScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState==RecyclerView.SCROLL_STATE_DRAGGING){
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager!=null){
                    View lastChild = layoutManager.getChildAt(layoutManager.getChildCount() - 1);
                    if (layoutManager.getPosition(lastChild)>20){
                        ViewCompat.animate(ivArrow).translationY(0).setDuration(500).start();
                    }else{
                        ViewCompat.animate(ivArrow).translationY(200).setDuration(500).start();
                    }
                }
            }
        }
    }
}
